package com.shujia.java.day21.demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class test {
    public static void main(String[] args) throws Exception{
        Class<?> student = Class.forName("com.shujia.java.day21.demo.Student");
        show(student);
        System.out.println("---------------");
        show1(student);
        System.out.println("---------------");
        show2(student);
        System.out.println("---------------");
        show2(student);
        System.out.println("---------------");
        show3(student);
        System.out.println("---------------");
        show4(student);
        System.out.println("---------------");
        show5(student);
        System.out.println("---------------");
        show6(student);
        System.out.println("---------------");
        show7(student);
        System.out.println("---------------");
        show8(student);
    }
    //反射创建对象
    public static void show(Class<?>  student) throws Exception{
        System.out.println(student.getName());
        Object object = student.newInstance();
        Student s=(Student)object;
        s.setName("张三");
        s.setAge(18);
        System.out.println(s.toString());
    }
    //反射获取里面的属性
    public static void show1(Class<?>  student) throws Exception{
        Field[] fields = student.getDeclaredFields();
        for (Field f:fields){
            //相当于把peivate改为public
            f.setAccessible(true);
            System.out.println(f);
        }
    }
    //反射获取里面的属性
    public static void show2(Class<?>  student) throws Exception{
        Field fields = student.getDeclaredField("name");
        System.out.println(fields);
    }
    //反射获取所有构造
    public static void show3(Class<?>  student) throws Exception{
        Constructor<?>[] dc = student.getConstructors();
        for (Constructor c:dc) {
            System.out.println(c);
        }
    }
    //反射获取单个构造并创建对象
    public static void show4(Class<?>  student) throws Exception{
        Constructor<?> dc = student.getConstructor(String.class,int.class);
        Object stu = dc.newInstance("张三", 14);
        Student s=(Student)stu;
        System.out.println(s.toString());
    }
    //反射获取所有方法
    public static void show5(Class<?>  student) throws Exception{
        //获取类中方法
        Method[] methods = student.getDeclaredMethods();
        for (Method m:methods) {
            System.out.println(m);
        }
    }
    //反射获取方法并赋值
    public static void show6(Class<?>  student) throws Exception{
        Method methods = student.getDeclaredMethod("getName");
        System.out.println(methods);

    }
    //反射调用私有方法
    public static void show7(Class<?>  student) throws Exception{
        Method methods = student.getDeclaredMethod("show");
        methods.setAccessible(true);
        //创建实例
        Object o = student.newInstance();
        methods.invoke(o);

    }
    //反射调用私有属性
    public static void show8(Class<?>  student) throws Exception{
        Field name = student.getDeclaredField("name");
        name.setAccessible(true);
        Object o = student.newInstance();
        name.set(o,"李四");
        System.out.println(name.get(o));

    }
}
